package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import beans.HistCommande;

public class DAOHistCommande extends DAO<HistCommande> {

	Connection laCo = null;
	PreparedStatement leStm = null;
	ResultSet result = null;
	
	@Override
	public HistCommande create(HistCommande obj) {
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("INSERT INTO histCom " +
					"(id_ingr, prixAchatActuel, qte, date) VALUES (?,?,?,?)");
			leStm.setInt(1, obj.getId_ingr());
			leStm.setDouble(2, obj.getPrixAchatActuel());
			leStm.setInt(3, obj.getQte());
			leStm.setDate(4,  new Date(obj.getDate().getTimeInMillis()));
			
			leStm.executeUpdate();
			
			leStm.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return obj;
	}

	@Override
	public HistCommande update(HistCommande obv) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(HistCommande obv) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public HistCommande find(long id) {
		HistCommande ret = null;
		try {
			laCo = dataSource.getConnection();
			leStm=laCo.prepareStatement("SELECT id,id_ingr,prixAchatActuel,qte,date " +
										"FROM histCom WHERE id=?");
			leStm.setLong(1, id);
			result = leStm.executeQuery();
		
			if(result.next()) {
				Date d = result.getDate(4);
				GregorianCalendar g = new GregorianCalendar();
				g.setTimeInMillis(d.getTime());
				
				ret = new HistCommande(result.getInt(1),result.getInt(2),
						result.getDouble(2), result.getInt(3), g);
			}
			
			leStm.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	@Override
	public List<HistCommande> findAll() {
		List<HistCommande> ret = null;
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("SELECT id,id_ingr,prixAchatActuel,qte,date " +
										"FROM histCom");
			
			result = leStm.executeQuery();
			
			ret = new ArrayList<HistCommande>();
			
			while(result.next()) {
				Date d = result.getDate(4);
				GregorianCalendar g = new GregorianCalendar();
				g.setTimeInMillis(d.getTime());
				
				ret.add(new HistCommande(result.getInt(1),result.getInt(2),
						result.getDouble(2), result.getInt(3), g));
			}
			
			leStm.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	public List<HistCommande> findByIngredient(int idIngr) {
		List<HistCommande> ret = null;
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("SELECT id,id_ingr,prixAchatActuel,qte,date " +
										"FROM histCom " +
										"WHERE id_ingr = ?");
			
			result = leStm.executeQuery();
			
			ret = new ArrayList<HistCommande>();
			
			while(result.next()) {
				Date d = result.getDate(4);
				GregorianCalendar g = new GregorianCalendar();
				g.setTimeInMillis(d.getTime());
				
				ret.add(new HistCommande(result.getInt(1),result.getInt(2),
						result.getDouble(2), result.getInt(3), g));
			}
			
			leStm.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ret;
	}

}
